package com.java110.mall.goods.bmo.housekeepingServ.impl;

import com.alibaba.fastjson.JSONArray;
import com.java110.core.annotation.Java110Transactional;
import com.java110.core.factory.GenerateCodeFactory;
import com.java110.dto.file.FileRelDto;
import com.java110.dto.housekeeping.HousekeepingServDto;
import com.java110.mall.goods.bmo.housekeepingServ.IUpdateHousekeepingServBMO;
import com.java110.intf.system.IFileInnerServiceSMO;
import com.java110.intf.system.IFileRelInnerServiceSMO;
import com.java110.intf.goods.IHousekeepingServInnerServiceSMO;
import com.java110.intf.goods.IProductDetailInnerServiceSMO;
import com.java110.intf.goods.IProductServKeywordInnerServiceSMO;
import com.java110.po.file.FileRelPo;
import com.java110.po.housekeeping.HousekeepingServPo;
import com.java110.po.product.ProductDetailPo;
import com.java110.po.product.ProductServKeywordPo;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;


@Service("updateHousekeepingServBMOImpl")
public class UpdateHousekeepingServBMOImpl implements IUpdateHousekeepingServBMO {

    @Autowired
    private IHousekeepingServInnerServiceSMO housekeepingServInnerServiceSMOImpl;
    @Autowired
    private IProductDetailInnerServiceSMO productDetailInnerServiceSMOImpl;
    @Autowired
    private IFileInnerServiceSMO fileInnerServiceSMOImpl;
    @Autowired
    private IFileRelInnerServiceSMO fileRelInnerServiceSMOImpl;
    @Autowired
    private IProductServKeywordInnerServiceSMO productServKeywordInnerServiceSMOImpl;
    /**
     *
     *
     * @param housekeepingServPo
     * @return 订单服务能够接受的报文
     */
    @Java110Transactional
    public ResponseEntity<String> update(String coverPhoto, JSONArray carouselFigurePhoto, ProductDetailPo productDetailPo,
                                         HousekeepingServPo housekeepingServPo, String keywords) {

        int flag = housekeepingServInnerServiceSMOImpl.updateHousekeepingServ(housekeepingServPo);

        if (flag < 1) {
            return ResultVo.createResponseEntity(ResultVo.CODE_ERROR, "修改失败");
        }

        //修改服务封面
        FileRelPo fileRelPo = new FileRelPo();
        fileRelPo.setObjId(housekeepingServPo.getServId());
        fileRelPo.setRelTypeCd(FileRelDto.REL_TYPE_CD_GOODS_COVER);
        fileRelInnerServiceSMOImpl.deleteFileRel(fileRelPo);
        doUdpateRentingFile(coverPhoto, housekeepingServPo, FileRelDto.REL_TYPE_CD_GOODS_COVER);
        //修改轮播图
        fileRelPo.setObjId(housekeepingServPo.getServId());
        fileRelPo.setRelTypeCd(FileRelDto.REL_TYPE_CD_GOODS_CAROUSEL_FIGURE);
        fileRelInnerServiceSMOImpl.deleteFileRel(fileRelPo);
        for (int carouselIndex = 0; carouselIndex < carouselFigurePhoto.size(); carouselIndex++) {
            doUdpateRentingFile(carouselFigurePhoto.getString(carouselIndex), housekeepingServPo, FileRelDto.REL_TYPE_CD_GOODS_CAROUSEL_FIGURE);
        }
        //修改服务详情
        productDetailPo.setProductId(housekeepingServPo.getServId());
        flag = productDetailInnerServiceSMOImpl.updateProductDetail(productDetailPo);
        if (flag < 1) {
            throw new IllegalArgumentException("修改服务详情失败");
        }
        //保存关键字
        doUpdatekeywords(keywords,housekeepingServPo);

        return ResultVo.createResponseEntity(ResultVo.CODE_OK, "修改成功");
    }
    /**
     * 保存房屋租赁 图片
     *
     * @param photo
     * @param housekeepingServPo
     */
    private void doUdpateRentingFile(String photo, HousekeepingServPo housekeepingServPo, String fileTypeCd) {



        FileRelPo fileRelPo = new FileRelPo();
        fileRelPo.setObjId(housekeepingServPo.getServId());
        fileRelPo.setRelTypeCd(fileTypeCd);
        fileRelPo.setFileRelId(GenerateCodeFactory.getGeneratorId(GenerateCodeFactory.CODE_PREFIX_relId));
        fileRelPo.setFileRealName(photo);
        fileRelPo.setFileSaveName(photo);
        fileRelPo.setSaveWay( FileRelDto.SAVE_WAY_FTP);
        int save = fileRelInnerServiceSMOImpl.saveFileRel(fileRelPo);

        if (save < 1) {
            throw new IllegalArgumentException("保存文件失败");
        }
    }

    /**
     * 保存房屋租赁 图片
     *
     * @param keywords
     */
    private void doUpdatekeywords(String keywords,HousekeepingServPo housekeepingServPo) {

        String [] keywor = keywords.replaceAll("；",";").split( ";" );

        ProductServKeywordPo servKeyword = new ProductServKeywordPo();
        servKeyword.setObjId( housekeepingServPo.getServId() );
        servKeyword.setShopId( housekeepingServPo.getShopId() );
        int deunt = productServKeywordInnerServiceSMOImpl.deleteProductServKeyword( servKeyword );
        if (deunt < 1) {
            throw new IllegalArgumentException("关键字删除失败");
        }
        for(String keyword:keywor){
            ProductServKeywordPo servKeywordPo = new ProductServKeywordPo();
            servKeywordPo.setObjId( housekeepingServPo.getServId() );
            servKeywordPo.setShopId( housekeepingServPo.getShopId() );
            servKeywordPo.setKeywordId(GenerateCodeFactory.getGeneratorId("13"));
            servKeywordPo.setKeywordName( keyword.trim() );
            servKeywordPo.setKeywordType( HousekeepingServDto.KEYWORD_SERV_TYPE_CD );
            int save =  productServKeywordInnerServiceSMOImpl.saveProductServKeyword( servKeywordPo );

            if (save < 1) {
                throw new IllegalArgumentException("关键字删除失败");
            }
        }
    }
}
